﻿#include "formvehilceinfo.h"
#include "ui_formvehilceinfo.h"
#include <QPixmap>
#include <QtGui>
#include <time.h>

FormVehicleInfo::FormVehicleInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormVehilceInfo)
{
    ui->setupUi(this);
    m_plblDesc=ui->label;
    m_plblAxleType=ui->label_2;
    m_plblDetectTime=ui->label_3;
    m_plblAxleNum=ui->label_4;
    m_plblAutoimg=ui->label_5;
    m_plblAutoVehType=ui->label_6;
    memset(&m_vehicle, 0, sizeof m_vehicle);
}

FormVehicleInfo::~FormVehicleInfo()
{
    delete ui;
}

void FormVehicleInfo::setDesc(const QString &sDesc)
{
    m_plblDesc->setText(sDesc);
}

QString Time2String(__int64 time)
{
    if (0==time)
    {
        return "";
    }
    char cTime[50];
    memset(cTime, 0, sizeof cTime);
    time_t tm=time;
    strftime(cTime, 32, "%H:%M:%S", localtime(&tm));
    return QString(cTime);
}

QString FormVehicleInfo::translateVehType(int nVehType)
{
    QString vehtype;
    if (nVehType>0 && nVehType<10)
    {
        //return tr("客%1").arg(nVehType);
        vehtype = tr("客%1").arg(nVehType);
    }
    else if (nVehType>0 && nVehType<20)
    {
        //return tr("货%1").arg(nVehType%10);
        vehtype =  tr("货%1").arg(nVehType%10);
    }
    //return "未知车型";
    else
    {
        vehtype = "未知车型";
    }
    currentvehtype = vehtype;
    return vehtype;
}

void FormVehicleInfo::updateVehTypeImg(int nVehType)
{
    // 查询图像文件名
    QString imgFileName;
    if (nVehType>0 && nVehType<10)
    {
        imgFileName=tr(":/images/ke%1.png").arg(nVehType);
    }
    else if (nVehType>0 && nVehType<20)
    {
        imgFileName=tr(":/images/huo%1.png").arg(nVehType);
        //imgFileName = ":/images/bug.png";
    }
    // 刷新图像
    if (imgFileName.length()>0)
    {
        QPixmap pixmap(60,40);
        pixmap.load(imgFileName);
        m_plblAutoimg->setPixmap(pixmap);
        m_plblAutoimg->setFixedSize(60,40);
        m_plblAutoimg->setScaledContents(true);
    }
    else
    {
        m_plblAutoimg->clear();
    }
}

void FormVehicleInfo::setAutoVehType(const VEHICLE &vehicle)
{

    memcpy(&m_vehicle, &vehicle, sizeof vehicle);
    m_plblAutoVehType->setText(translateVehType(::translateVehType(vehicle)));
    updateVehTypeImg(::translateVehType(vehicle));
    m_plblDetectTime->setText(Time2String(vehicle.tTransit));
    if (vehicle.bDualTyre)
    {
        m_plblAxleNum->setText(tr("%1轴双胎").arg(vehicle.nAxle));
    }
    else
    {
        m_plblAxleNum->setText(tr("%1轴单胎").arg(vehicle.nAxle));
    }
    m_plblAxleType->setText(vehicle.szAxleType);
}
void FormVehicleInfo::clearVehInfo()
{
    memset(&m_vehicle, 0, sizeof m_vehicle);
    m_plblAutoVehType->clear();
    m_plblDetectTime->clear();
    m_plblAxleNum->clear();
    m_plblAxleType->clear();
    m_plblAutoimg->clear();
}

void FormVehicleInfo::showMiniMode()
{
    m_plblDesc->hide();
    m_plblAxleType->hide();
    m_plblDetectTime->hide();
    m_plblAxleNum->hide();

}


int translateVehType(const VEHICLE &veh)
{
    if (veh.nClass<=0)
    {
        return 0;
    }
    else if (veh.nClass<=4)
    {
        return veh.nClass;
    }
    else if (veh.nClass<=10)
    {
        return 4;
    }
    else if (veh.nClass == 11 || veh.nClass==15)
    {
        return veh.nClass;
    }
    else if(veh.nClass > 11 && veh.nClass < 15)
    {
        QSettings settings(".\\AutoVehType.ini", QSettings::IniFormat);
        int nFlag = settings.value("DetectType/VALUE").toInt();

        if(nFlag == 1)
        {
            return 10 + veh.nAxle;
        }
        else
        {
            return veh.nClass;
        }
    }
    else
    {
        return 15;
    }
}


